package com.market.product.server.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.market.common.log.annotation.Log;
import com.market.common.log.enums.BusinessType;
import com.market.common.security.annotation.RequiresPermissions;
import com.market.product.common.domain.ProductSkuInfo;
import com.market.product.server.service.IProductSkuInfoService;
import com.market.common.core.web.controller.BaseController;
import com.market.common.core.utils.poi.ExcelUtil;
import com.market.common.core.web.page.TableDataInfo;
import com.market.common.core.domain.Result;

/**
 * 商品SKUController
 *
 * @author market
 * @date 2024-02-27
 */
@RestController
@RequestMapping("/productSkuInfo")
public class ProductSkuInfoController extends BaseController
{
    @Autowired
    private IProductSkuInfoService productSkuInfoService;

    /**
     * 查询商品SKU列表
     */
    @RequiresPermissions("product:productSkuInfo:list")
    @GetMapping("/list")
    public Result<TableDataInfo> list(ProductSkuInfo productSkuInfo)
    {
        startPage();
        List<ProductSkuInfo> list = productSkuInfoService.selectProductSkuInfoList(productSkuInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品SKU列表
     */
    @RequiresPermissions("product:productSkuInfo:export")
    @Log(title = "商品SKU", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSkuInfo productSkuInfo)
    {
        List<ProductSkuInfo> list = productSkuInfoService.selectProductSkuInfoList(productSkuInfo);
        ExcelUtil<ProductSkuInfo> util = new ExcelUtil<ProductSkuInfo>(ProductSkuInfo.class);
        util.exportExcel(response, list, "商品SKU数据");
    }

    /**
     * 获取商品SKU详细信息
     */
    @RequiresPermissions("product:productSkuInfo:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {

        return success(productSkuInfoService.selectProductSkuInfoById(id));
    }

    /**
     * 新增商品SKU
     */
    @RequiresPermissions("product:productSkuInfo:add")
    @Log(title = "商品SKU", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody ProductSkuInfo productSkuInfo)
    {
        return toAjax(productSkuInfoService.insertProductSkuInfo(productSkuInfo));
    }

    /**
     * 修改商品SKU
     */
    @RequiresPermissions("product:productSkuInfo:edit")
    @Log(title = "商品SKU", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody ProductSkuInfo productSkuInfo)
    {
        return toAjax(productSkuInfoService.updateProductSkuInfo(productSkuInfo));
    }

    /**
     * 删除商品SKU
     */
    @RequiresPermissions("product:productSkuInfo:remove")
    @Log(title = "商品SKU", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(productSkuInfoService.deleteProductSkuInfoByIds(ids));
    }

    /**
     * 更新库存
     */

    @RequiresPermissions("product:product:updateProductInventory")
    @Log(title = "更新库存", businessType = BusinessType.UPDATE)
    @GetMapping("updateProductInventory/{productId}/{stock}")
    public Result updateProductInventory(@PathVariable("productId") Integer productId,@PathVariable("stock") Integer stock)
    {
        return productSkuInfoService.updateProductInventory(productId,stock);
    }

    /*
    *
    * 根据id查询商品价格
    * */
    @PostMapping("/queryPriceBySkuId/{id}")
    public Result queryPriceBySkuId(@PathVariable("id") Long id){
        return success(productSkuInfoService.queryPriceBySkuId(id));
    }


}
